[globalNotificationId].page.tsx 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import { useEffect } from 'react';
  2. import dynamic from 'next/dynamic';
  3. import { useRouter } from 'next/router';
  4. import { objectIdUtils } from '@growi/core/dist/utils';
  5. import type { NextPageWithLayout } from '../../_app.page';
  6. import type { AdminCommonProps } from '../_shared';
  7. import {
  8. createAdminPageLayout,
  9. getServerSideAdminCommonProps,
  10. } from '../_shared';
  11. const ManageGlobalNotification = dynamic(
  12. () =>
  13. import('~/client/components/Admin/Notification/ManageGlobalNotification'),
  14. { ssr: false },
  15. );
  16. type Props = AdminCommonProps;
  17. const AdminGlobalNotificationDetailPage: NextPageWithLayout<Props> = () => {
  18. const router = useRouter();
  19. const { globalNotificationId } = router.query;
  20. const currentGlobalNotificationId = Array.isArray(globalNotificationId)
  21. ? globalNotificationId[0]
  22. : globalNotificationId;
  23. useEffect(() => {
  24. const toastErrorPromise = import('~/client/util/toastr').then(
  25. (mod) => mod.toastError,
  26. );
  27. if (globalNotificationId == null) {
  28. router.push('/admin/notification');
  29. return;
  30. }
  31. if (
  32. currentGlobalNotificationId != null &&
  33. !objectIdUtils.isValidObjectId(currentGlobalNotificationId)
  34. ) {
  35. (async () => {
  36. (await toastErrorPromise)('Invalid notification id');
  37. router.push('/admin/global-notification/new');
  38. })();
  39. }
  40. }, [currentGlobalNotificationId, globalNotificationId, router]);
  41. return currentGlobalNotificationId != null && router.isReady ? (
  42. <ManageGlobalNotification
  43. globalNotificationId={currentGlobalNotificationId}
  44. />
  45. ) : null;
  46. };
  47. AdminGlobalNotificationDetailPage.getLayout = createAdminPageLayout<Props>({
  48. title: (_p, t) => t('external_notification.external_notification'),
  49. containerFactories: [
  50. async () => {
  51. const AdminNotificationContainer = (
  52. await import('~/client/services/AdminNotificationContainer')
  53. ).default;
  54. return new AdminNotificationContainer();
  55. },
  56. ],
  57. });
  58. export const getServerSideProps = getServerSideAdminCommonProps;
  59. export default AdminGlobalNotificationDetailPage;